package com.example.gameapplication.bullet;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;

import com.example.gameapplication.BaseObject;
import com.example.gameapplication.R;
import com.example.gameapplication.database.DataUtils;
import com.example.gameapplication.plant.BossPlane;

public class BossSkillBullet extends BaseObject {
    private BossPlane bossPlane;
    double xSpeed;//x轴的速度，正数向右，负数向左
    double ySpeed;
    double ra;
    private Bitmap mBullet;
    private int currentBulletBitmapId=0;

    public BossSkillBullet(Resources resources, Paint paint, int Speed, double ra) {//子弹刚刚发出的一刻就要知道图片的倾斜角度，所以需要这些参数
        super(resources, paint);
        this.speed = Speed;
        this.ra = ra;
        this.lv =1;//发射的时候敌人Boss等级决定
        mainPlane=DataUtils.getmainPlane(resources);
        initEnemyBitmap();
    }


    public void setLv(int lv) {
        if (getLv() != lv) {
            this.lv = lv;
            initEnemyBitmap();
        }
    }

    public void initEnemyBitmap() {
        xSpeed = Math.sin(ra) * speed;//sin带正负 Math.PI是根据所给图片样式决定的，因为本项目默认的子弹都是向着上方的
        ySpeed = -Math.cos(ra) * speed;
        if (getLv()<4){
            if ( currentBulletBitmapId == R.drawable.enemy_bullet&&mBullet!=null&&!mBullet.isRecycled())return;
            currentBulletBitmapId = R.drawable.enemy_bullet;
        }else if (getLv()<7){
            if ( currentBulletBitmapId == R.drawable.boss_bullet_sun_particle&&mBullet!=null&&!mBullet.isRecycled())return;
            currentBulletBitmapId=R.drawable.boss_bullet_sun_particle;
        }else if (getLv()<10){
            if ( currentBulletBitmapId == R.drawable.boss_bullet_thunderball_green&&mBullet!=null&&!mBullet.isRecycled())return;
            currentBulletBitmapId= R.drawable.boss_bullet_thunderball_green;
        }else if (getLv()<13){
            if ( currentBulletBitmapId == R.drawable.boss_bullet_hellfire_yellow&&mBullet!=null&&!mBullet.isRecycled())return;
            currentBulletBitmapId= R.drawable.boss_bullet_hellfire_yellow;
        }else if (getLv()<16){
            if ( currentBulletBitmapId == R.drawable.boss_bullet_hellfire_red&&mBullet!=null&&!mBullet.isRecycled())return;
            currentBulletBitmapId= R.drawable.boss_bullet_hellfire_red;
        }else {
            if ( currentBulletBitmapId == R.drawable.my_bullet_red&&mBullet!=null&&!mBullet.isRecycled())return;
            currentBulletBitmapId= R.drawable.my_bullet_red;
        }
        if (mBullet != null && !mBullet.isRecycled()) mBullet.recycle();
        mBullet = rotateBmp(BitmapFactory.decodeResource(resources,currentBulletBitmapId), Math.toDegrees(ra));
        object_width = mBullet.getWidth();
        object_height = mBullet.getHeight();
    }

    @Override
    public void setAlive(boolean alive){
        BossPlane currentBossPlane = DataUtils.getCurrentBossPlane();
        if (currentBossPlane != null) {
            super.setAlive(alive);
            setSeat(currentBossPlane.midx, currentBossPlane.midy);
        }
    }

    @Override
    public void initBitmap() {
    }

    @Override
    public void drawSelf(Canvas canvas) {

    }

    /**
     * 旋转bitmap
     *
     * @param b
     * @param degrees
     * @return
     */
    public static Bitmap rotateBmp(Bitmap b, double degrees) {
        if (degrees != 0 && b != null) {
            Matrix m = new Matrix();
            m.setRotate((float) degrees, (float) b.getWidth() / 2,
                    (float) b.getHeight() / 2);
            try {
                Bitmap b2 = Bitmap.createBitmap(b, 0, 0, b.getWidth(),
                        b.getHeight(), m, true);
                if (b != b2) {
                    b.recycle();
                    b = b2;
                }
            } catch (OutOfMemoryError ex) {
                ex.printStackTrace();
            }
        }
        return b;
    }

    public int drawSkillSelf(Canvas canvas) {
        if (!isAlive()) return 1;
        setSeat(midx, midy);
        //画子弹
        canvas.save();
        canvas.clipRect(leftx, lefty, rightx, righty);
        canvas.drawBitmap(mBullet, leftx, lefty, paint);
        canvas.restore();
        logic();
//        attack();// DataUtils.runbossSkillAttack();
        return 0;
    }



    /**
     * 因为这种判断只要bitmap的边界接触就会死亡，但是部分bitmap的边缘是透明的,容易产生未击中已经死亡的感觉
     */
//    private void attack() {
//        if (isAlive() && testPlane.unbeatable <= 0 && rightx > testPlane.leftx && leftx < testPlane.rightx && righty > testPlane.lefty && lefty < testPlane.righty) {
//            //击中testPlane了
//            setAlive(false);//子弹死亡
//            if (testPlane.lv > 1) {//最低1级，伤害是等级的倍数，所以不可以为0,也可以看着0生命值就是死亡
//                testPlane.lv--;
//                testPlane.unbeatable = 100;
//            } else {
//                testPlane.setAlive(false);
//            }
//        }
//    }
    private void attack() {

            DataUtils.bossSkillAttack(this);

    }

    public void logic() {//子弹的逻辑，产生之后，x不变，y轴变化

        if (midy < 0 || midy > mainPlane.screen_height || midx < 0 || midx > mainPlane.screen_width) {//离开屏幕
            setAlive(false);
//            setSeat(testPlane.midx,testPlane.midy);
            return;
        }
        if (isAlive())//子弹击中目标，则在离开屏幕之前就被销毁了
        {
            midy = (float) (midy + ySpeed);
            midx = (float) (midx + xSpeed);
        }
    }

    @Override
    public void dataDestroyed() {
        if (!mBullet.isRecycled()) {
            mBullet.recycle();
        }
    }
}
